// pages/index/law_service/leave_messages/add/add.js
import Dialog from "../../../../../miniprogram_npm/@vant/weapp/dialog/dialog";
import Toast from "../../../../../miniprogram_npm/@vant/weapp/toast/toast";

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    textareaValue: null,//描述信息
    fileList: [],
    mediate_code: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
  },

  /**
   * 设置事件描述 输入框中的值
   */
  getTextareaValue: function (e) {
    this.setData({
      textareaValue: e.detail.value
    });
  },
  /**
   * 删除图片
   */
  deleteImage: function (e) {
    const than = this
    const index = e.detail.index
    this.data.fileList.splice(index, 1)
    than.setData({
      fileList: than.data.fileList
    })

  },
  /**
   * 图片上传
   */
  afterRead: function (e) {
    const than = this
    // e.detail.file.forEach(item => {
    //   http.upFile(item.path,"msg").then(res => {
    //     if (res.code === http.REQUEST_OK){
    //       than.data.fileList.push({
    //         url: res.data.url,
    //         name: item.size
    //       })
    //       than.setData({
    //         fileList: than.data.fileList
    //       })
    //     }
    //   })
    // })
    for (let i = 0; i < e.detail.file.length; i++) {
      than.data.fileList.push({
        url: e.detail.file[i].path,
        name: e.detail.file[i].size
      })
    }
    than.setData({
      fileList: than.data.fileList
    })
  },

  /**
   * 发布留言
   */
  onLeavingMessage: function () {
    const than = this
    const fileList1 = than.data.fileList
    let fileArray = []
    Dialog.confirm({
      title: "提示",
      message: "确定要发布留言吗？"
    }).then(() => {
      //确认按钮
      Toast.loading({
        duration: 3000,
        forbidClick: true,
        message: '发布中...'
      })
      //先上传文件
      let upFileNumber = 0
      for (let i = 0; i < fileList1.length; i++) {
        http.upFile(fileList1[i].url, "msg").then(res => {
          fileArray[i] = res.data.url
          upFileNumber++
        },err => {
          upFileNumber++
        })
      }
      let temp = setInterval(() => {
        if (upFileNumber >= fileList1.length){
          clearInterval(temp)
          log.e("开始提交")
          http.postJson("information_reply/add", {
            post_id: than.data.id,
            reply_user_id: app.globalData.userInfo.applet_user_id,
            reply_user_name: wx.getStorageSync("name"),
            reply_content: than.data.textareaValue,
            enclosure: fileArray,
            phone: wx.getStorageSync("phone")
          }).then(res => {
            if (res.data.code === http.REQUEST_OK) {
              Toast.success(res.data.msg);
              setTimeout(res => {
                wx.navigateBack({})
              }, 1000)
            } else {
              Toast.fail(res.data.msg);
            }
          },err => {
            Toast.fail("网络异常");
          })
        }
      },500)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})